package de.ur.iw.wiki.test;

import de.ur.iw.wiki.Link;
import de.ur.iw.wiki.Paragraph;
import de.ur.iw.wiki.Settings;
import de.ur.iw.wiki.Weighter;
import de.ur.iw.wiki.WikiPage;
import de.ur.iw.wiki.weighters.TomsWeighter;

public class WeightTester {

	public static Weighter weighter = new TomsWeighter();

	public static void main(String[] args) {

		Settings.setKey(Settings.KEY_CATEGORIES_WEIGHT, .5);

		try {
			WikiPage page = DownloadTester.pageTest();
			page.weight(weighter);
			print(page, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void print(WikiPage page, boolean weightOnly) {
		System.out.println("there are " + page.size() + " links in '"
				+ page.getName() + "\n" + "in " + page.getParagraphCount()
				+ " Paragraphs:");

		for (int i = 0; i < page.getParagraphCount(); i++) {
			Paragraph p = page.getParagraphAt(i);
			printParagraph(p, 1, weightOnly);
		}

		System.out.println("containing " + page.getFileCount() + " files:");
		for (int i = 0; i < page.getFileCount(); i++) {
			if (weightOnly) {
				printWeight(page.getFileAt(i).getWeight());
			} else {
				System.out.println("\t" + page.getFileAt(i).getName()
						+ " (weight: " + getFormattedWeight(page.getFileAt(i).getWeight()) + ")");
			}
		}

		System.out.println("and " + page.getCategoryCount() + " categories:");
		for (int i = 0; i < page.getCategoryCount(); i++) {
			Link link = page.getCategoryAt(i);
			if (weightOnly) {
				printWeight(link.getWeight());
			} else {
				System.out.println("\t" + link.getName() + " (weight: "
						+ getFormattedWeight(link.getWeight()) + ")");
			}
		}
	}

	private static void printParagraph(Paragraph p, int deepness,
			boolean weightOnly) {
		String tabs = "";
		if (!weightOnly) {
			for (int i = 0; i < deepness; i++) {
				tabs += "\t";
			}
		}
		System.out.println(tabs + p.getName() + " (" + p.getAllLinks().size()
				+ " links)");
		if (p.getParagraphCount() > 0) {
			for (int i = 0; i < p.getParagraphCount(); i++) {
				printParagraph((Paragraph) p.getParagraphAt(i), deepness + 1,
						weightOnly);
			}
		} else {
			for (int i = 0; i < p.getAllLinks().size(); i++) {
				Link link = p.getAllLinks().get(i);
				if (weightOnly) {
					printWeight(link.getWeight());
				} else {
					System.out.println(tabs + "\t" + link.getName()
							+ " (weight: " +getFormattedWeight(link.getWeight()) + ")");
				}
			}
		}
	}

	private static void printWeight(double weight) {
		String str = "weight: ";
		for (int i = 0; i < weight * 100; i++) {
			str += "#";
		}
		System.out.println(str+"  "+getFormattedWeight(weight));
	}
	
	private static String getFormattedWeight(double weight){
		return Math.rint(weight*10000)/100+"%";
	}

}
